package com.bestcem.xm.cjm.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bestcem.xm.cjm.entity.CjmPackage;
import com.bestcem.xm.cjm.mapper.CjmPackageMapper;
import com.bestcem.xm.cjm.service.CjmPackageService;
import com.bestcem.xm.common.core.domain.web.XmResultJson;
import com.bestcem.xm.component.mybatis.service.impl.XmServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@RequiredArgsConstructor
@Service
public class CjmPackageServiceImpl extends XmServiceImpl<CjmPackageMapper, CjmPackage> implements CjmPackageService {

    @Override
    public XmResultJson<?> updatePackage(String id, JSONObject param) {
        CjmPackage update = param.toJavaObject(CjmPackage.class);
        update.setId(id);
        baseMapper.updateById(update);

        JSONObject ret = new JSONObject();
        ret.put("id", id);
        return XmResultJson.success(ret);
    }

    @Override
    public XmResultJson<?> deletePackageById(String id) {
        JSONObject ret = new JSONObject();
        ret.put("deletedCount", baseMapper.deleteById(id));
        return XmResultJson.success(ret);
    }

    @Override
    public XmResultJson<?> deletePackages(JSONObject baseParam, JSONObject param) {
        CjmPackage query = baseParam.toJavaObject(CjmPackage.class);
        LambdaQueryWrapper<CjmPackage> wrapper = new LambdaQueryWrapper<>(query);
        wrapper.in(CjmPackage::getId, param.getJSONArray("idList"));
        JSONObject ret = new JSONObject();
        ret.put("deletedCount", baseMapper.delete(wrapper));
        return XmResultJson.success(ret);
    }

    @Override
    public XmResultJson<?> addPackage(JSONObject params) {
        CjmPackage info = params.toJavaObject(CjmPackage.class);
        info.setOrder(0);
        baseMapper.insert(info);
        JSONObject ret = new JSONObject();
        ret.put("id", info.getId());
        return XmResultJson.success(ret);
    }
}
